package com.tiger.userClient.http.card;

import com.alibaba.fastjson2.JSONObject;
import com.tiger.cache.RedisService;
import com.tiger.userClient.http.card.dto.PageDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CardService {
    @Value("${openAccountConfig.tokenKey}")
    private String tokenKey;

    @Value("${openAccountConfig.apiUrl}")
    private String apiUrl;


    @Value("${openAccountConfig.cardApiId}")
    private String cardApiId;

    @Value("${openAccountConfig.cardLogApiId}")
    private String cardLogApiId;

    private final RedisService redisService;

    public CardService(RedisService redisService) {
        this.redisService = redisService;
    }
    public Object card(String userKey) { //230774002003

        RestTemplate restTemplate = new RestTemplate();

        JSONObject jsonStr = new JSONObject();
        jsonStr.put("id", cardApiId);


        List<HashMap<String, Object>> params = new ArrayList<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("xh", userKey);

        params.add(param);

        jsonStr.put("params", params);
        jsonStr.put("pageSize", 1);
        jsonStr.put("pageIndex", 1);

        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiUrl);
        builder.queryParam("token", redisService.template.opsForValue().get(tokenKey));
        builder.queryParam("jsonStr", jsonStr.toJSONString());



        URI uri = builder.build().encode().toUri();
        HashMap r = restTemplate.getForEntity(uri, HashMap.class).getBody();


        HashMap<String, Object> data = (HashMap<String, Object>) r.get("data");
        if(data == null) return null;

        List<HashMap> list = (List<HashMap>) data.get("list");
        if(list == null || list.isEmpty()) return null;

        return list.get(0);
    }

    public Object log(PageDto dto, String userKey) {

        RestTemplate restTemplate = new RestTemplate();

        JSONObject jsonStr = new JSONObject();
        jsonStr.put("id", cardLogApiId);



        List<HashMap<String, Object>> params = new ArrayList<>();
        HashMap<String, Object> param = new HashMap<>();
        param.put("xh", userKey);
        param.put("sj1", dto.getStart());
        param.put("sj2", dto.getEnd());

        params.add(param);

        jsonStr.put("params", params);

        jsonStr.put("pageSize", dto.getPageSize());
        jsonStr.put("pageIndex", dto.getPageNo());

        System.out.println(jsonStr);


        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(apiUrl);
        builder.queryParam("token", redisService.template.opsForValue().get(tokenKey));
        builder.queryParam("jsonStr", jsonStr.toJSONString());



        URI uri = builder.build().encode().toUri();
        HashMap r = restTemplate.getForEntity(uri, HashMap.class).getBody();
        HashMap<String, Object> data = (HashMap<String, Object>) r.get("data");

        System.out.println(data);

        return data.get("list");
    }
}
